package com.aistar.service.impl;

import com.aistar.mapper.QuestionMapper;
import com.aistar.mapper.TracommMapper;
import com.aistar.mapper.TravelnoteMapper;
import com.aistar.pojo.*;
import com.aistar.service.MemberService;
import com.aistar.service.TracommService;
import com.aistar.service.TravelnoteService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class TravelnoteServiceImpl implements TravelnoteService {

    @Autowired
    private TravelnoteMapper travelnoteMapper;
    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private MemberService memberService;

    @Autowired
    private TracommService tracommService;
    @Autowired
    private TracommMapper tracommMapper;

    @Override
    public TravelCompletely getbyId(int id) {
        //根据id查询游记所有信息
        Travelnote travelnote = travelnoteMapper.selectByPrimaryKey(id);

        //根据游记id得到用户id，查询用户信息
        Member member = memberService.getbyId(travelnote.getTravWid());

        //根据游记id得到所有评论信息
        List<Tracomm> tracommList = tracommService.getbyTravelId(travelnote.getTravId());

        List<CommMember> commMemberList = new ArrayList<>();

        for(Tracomm trac:tracommList){
            CommMember commMember = new CommMember();
            Member memberuser = memberService.getbyId(trac.getTracUserId());//评论用户的信息
            commMember.setCommmember(memberuser);
            commMember.setTracomm(trac);//评论的信息
            commMemberList.add(commMember);
        }


     TravelCompletely travelCompletely = new TravelCompletely(travelnote,member,commMemberList
     );
        return travelCompletely;
    }

    @Override
    //分页查询所有游记按点赞数量降序排序
    public PageInfo<Travelnote> getAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);

        TravelnoteExample example = new TravelnoteExample();
        example.setOrderByClause("trav_praise desc");

        List<Travelnote> travelList = travelnoteMapper.selectByExample(example);

        return new PageInfo<Travelnote>(travelList);
    }



    @Override
    //分页查询所有游记按时间顺序升序排序
    public PageInfo<Travelnote> getAllfromdata(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);

        TravelnoteExample example = new TravelnoteExample();
        example.setOrderByClause("trav_id desc");

        List<Travelnote> travelList = travelnoteMapper.selectByExample(example);

        return new PageInfo<Travelnote>(travelList);
    }

    @Override
    public List<Travelnote> getfivetravel() {

        List<Travelnote> travelnoteList = new ArrayList<>();
        for(int i=1;i<=5;i++)
            travelnoteList.add(travelnoteMapper.selectByPrimaryKey(i));

        return travelnoteList;
    }

    @Override
    public int saveOneTravel(Travelnote travelnote) {
        //添加数据
        int a = travelnoteMapper.insert(travelnote);

        return a;
    }

    @Override
    public int saveOneQues(Question question) {
       //添加记录到问表中
        int A =questionMapper.insert(question);
        return A;
    }

    @Override
    public int savetracomm(Tracomm tracomm) {
        //游记中添加评论
        int T = tracommMapper.insert(tracomm);
        return 0;
    }

    @Override
    //通过id来查找游记的集合
    public List<Travelnote> getByuserid(int id) {
        //新增通过用户id查找游记的字段
        TravelnoteExample example = new TravelnoteExample();
        TravelnoteExample.Criteria criteria = example.createCriteria();
        criteria.andTravWidEqualTo(id);

        List<Travelnote> travelnoteList = travelnoteMapper.selectByExampleWithBLOBs(example);

        return travelnoteList;
    }
}
